package com.aholu.wicket.pages;

import java.util.Date;
import java.util.List;

import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.aholu.dao.DaoFactory;
import com.aholu.dao.UserDao;
import com.aholu.model.User;
import com.aholu.wicket.WebSession;

@AuthorizeInstantiation("admin")
public class AdminPage extends WebPage {

	final PageableListView<User> listView;
	final List<User> users;
	private Date date;

	public List<User> getUsers() {
		return users;
	}

	public Date getDate() {
		return date;
	}

	public AdminPage() {

		UserDao userDao = new UserDao();
		User user = ((WebSession) AuthenticatedWebSession.get()).getLoggedUser();
		date = user.getDate();

		users = userDao.findAll();
		add(listView = new PageableListView<User>("users", new PropertyModel<List<User>>(this, "users"), 15) {
			@Override
			public void populateItem(final ListItem<User> listItem) {
				final User user = listItem.getModelObject();
				listItem.add(new Label("userName", new PropertyModel(user, "userName")));
				listItem.add(new Label("name", new PropertyModel(user, "name")));
				listItem.add(new Label("surName", new PropertyModel(user, "surName")));

				listItem.add(new Link<String>("edit") {

					@Override
					public void onClick() {
						User u = (User) getParent().getDefaultModelObject();
						setResponsePage(new EditPage(u));
					}

				});
				listItem.add(new Link<String>("delete") {

					@Override
					public void onClick() {
						User u = (User) getParent().getDefaultModelObject();
						DaoFactory.getUserDao().makeTransient(u);
						setResponsePage(AdminPage.class);

					}

				});
			}
		});

		final TextField<String> username = new TextField<String>("username", Model.of(""));
		username.setModelValue(new String[] { user.getUserName() });
		final TextField<String> name = new TextField<String>("name", Model.of(""));
		name.setModelValue(new String[] { user.getName() });
		final TextField<String> surname = new TextField<String>("surname", Model.of(""));
		surname.setModelValue(new String[] { user.getSurName() });
		final TextField<String> email = new TextField<String>("email", Model.of(""));
		email.setModelValue(new String[] { user.getEmail() });
		final TextField<String> password = new TextField<String>("password", Model.of(""));
		password.setModelValue(new String[] { user.getPassword() });
		final DateTextField dateTextField = new DateTextField("dateTextField", new PropertyModel<Date>(this, "date")) {

		};
		final Link<String> link = new Link<String>("button") {

			@Override
			public void onClick() {
				setResponsePage(RegisterPage.class);
			}
		};
		final Link<String> exitLink = new Link<String>("exit") {

			@Override
			public void onClick() {
				setResponsePage(LoginPage.class);
			}
		};

		Form<?> form = new Form<Void>("editForm") {

			@Override
			protected void onSubmit() {

				setResponsePage(EditPage.class);

			}
		};

		form.add(username);
		form.add(name);
		form.add(surname);
		form.add(email);
		form.add(password);
		form.add(dateTextField);
		form.add(link);
		add(exitLink);
		add(form);

	}
}
